package com.bayihouse.utils;

import com.baomidou.mybatisplus.extension.service.IService;
import com.bayihouse.model.excel.publicClass.father.BaseEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


public class SqlUtils {

    /**
     * 校验排序字段是否合法（防止 SQL 注入）
     *
     * @param sortField
     * @return
     */
    public static boolean validSortField(String sortField) {
        if (StringUtils.isBlank(sortField)) {
            return false;
        }
        return !StringUtils.containsAny(sortField, "=", "(", ")", " ");
    }


    public static <T extends BaseEntity> List<T> getNotInsertList(List<T> list) {
        List<T> collect = list.stream().filter(object -> object.isNotInsert()).collect(Collectors.toList());
        return collect;
    }


    public static <T extends BaseEntity> boolean insertDataBase(List<T> list, IService<T> service) {
        List<T> notInsertList = getNotInsertList(list);
        boolean b = service.saveBatch(notInsertList);
        notInsertList.forEach(object -> object.setStatus(1));
        return b;
    }
}
